package cn.openea.perpartner.componet.config;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class AliyunConfig {
    @Value("${aliyun.sms.accessKeyId}")
    private String accessKeyId;

    @Value("${aliyun.sms.accessKeySecret}")
    private String accessKeySecret;

    @Value("${aliyun.sms.endpoint}")
    private String endpoint;

    /**
     * 阿里云短信服务
     * @return
     * @throws ClientException
     */
    @Bean
    public IAcsClient acsClient() throws ClientException {
        IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
        DefaultProfile.addEndpoint(endpoint, "cn-hangzhou", "Dysmsapi", endpoint);
        return new DefaultAcsClient(profile);
    }

    /**
     * 在 Spring Boot 应用启动时调用一次
     * 将 Endpoint 添加到 SDK 的 Endpoint 列表中。
     * 这样，在调用短信服务的 API 时，SDK 就知道应该使用哪个 Endpoint。
     */
    @Bean
    public CommandLineRunner commandLineRunner() {
        return args -> DefaultProfile.addEndpoint(endpoint, "cn-hangzhou", "Dysmsapi", endpoint);
    }
}

